package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.graphics.Rect
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import com.android.kotlincoroutinemvvmdemo.R

class DoubleSimiCircleView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : SurfaceViewTemplate(context, attributeSet) {

    private val mPaint by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            //style = Paint.Style.STROKE
            style = Paint.Style.FILL
            color = context.resources.getColor(R.color.color_F1501E)
        }
    }

    private val mPath by lazy {
        Path()
    }

    init {
        mHolder.addCallback(this)
    }

    override fun Canvas.onDrawContent() {
        val cx = mWidth.toFloat() / 2
        val cy = mHeight.toFloat() / 2

        translate(cx, cy)

        drawColor(context.resources.getColor(R.color.color_F3F3F3))
        val mStrokeWidth = (cx * 1 / 20).coerceAtLeast(minStrokeWidth)
        mPaint.strokeWidth = mStrokeWidth

        mPath.apply {
            addArc(
                -cx + mStrokeWidth / 2,
                -cy * 3 / 8,
                -cx / 4 + mStrokeWidth / 2,
                cy * 3 / 8,
                90F,
                180F,
            )

            lineTo(
                cx * 5 / 8,
                -cy * 3 / 8
            )

            addArc(
                cx / 4 - mStrokeWidth / 2,
                -cy * 3 / 8,
                cx - mStrokeWidth / 2,
                cy * 3 / 8,
                270F,
                180F,
            )

            lineTo(
                -cx * 5 / 8,
                cy * 3 / 8
            )
        }

        drawPath(mPath, mPaint)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        when (event?.action) {
            MotionEvent.ACTION_DOWN -> {
                mPaint.color = context.resources.getColor(R.color.color_64C630)
                invalidate()
            }
            MotionEvent.ACTION_UP -> {
                mPaint.color = context.resources.getColor(R.color.color_F1501E)
                invalidate()
            }
        }
        return super.onTouchEvent(event)
    }
}